package com.chc.config.security.detailservice;

import com.chc.web.menu.entity.Menu;
import com.chc.web.menu.service.MenuService;
import com.chc.web.reader.entity.Reader;
import com.chc.web.reader.service.ReaderService;
import com.chc.web.user.entity.User;
import com.chc.web.user.service.UserService;
import org.aspectj.lang.annotation.Around;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA
 *
 * @Author: chc
 * @Date: 2022/04/24/14:46
 * @Description:
 */
@Component("custometUserDetailService")
public class CustometUserDetailService implements UserDetailsService {

    @Autowired
    private UserService userService;

    @Autowired
    private ReaderService readerService;

    @Autowired
    private MenuService menuService;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //认证
        int index = s.indexOf(":");
        String username = s.substring(0,index);
        String userType = s.substring(index+1,s.length());
        if(userType.equals("0")){ //读者
            Reader reader = readerService.loadUser(username);
            if (reader == null){
                throw new UsernameNotFoundException("用户帐户不存在!");
            }
            //查询用户权限
            List<Menu> menuList = menuService.getMenuListByUserIdForReader(reader.getId());
            List<String> collect = menuList.stream().filter(item -> item != null).map(item -> item.getMenuCode())
                    .filter(item -> item != null).collect(Collectors.toList());
            //转成数组
            String[] strings = collect.toArray(new String[collect.size()]);
            List<GrantedAuthority> authorityList = AuthorityUtils.createAuthorityList(strings);
            //设置用户权限
            reader.setAuthorities(authorityList);
            return reader;
        }else if(userType.equals("1")) { //管理员
            User user = userService.loadUser(username);
            if (user == null){
                throw new UsernameNotFoundException("用户帐户不存在!");
            }
            //查询用户权限
            List<Menu> menuList = menuService.getMenuListByUserId(user.getUserId());
            List<String> collect = menuList.stream().filter(item -> item != null).map(item -> item.getMenuCode())
                    .filter(item -> item != null).collect(Collectors.toList());
            //转成数组
            String[] strings = collect.toArray(new String[collect.size()]);
            List<GrantedAuthority> authorityList = AuthorityUtils.createAuthorityList(strings);
            //设置用户权限
            user.setAuthorities(authorityList);
            return user;
        }else{
            throw new UsernameNotFoundException("用户类型不存在!");
        }

    }
}
